<div style="margin:50px 50px 50px 50px">
    <form class="layui-form layui-form-pane" lay-filter="example">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label"><i class="ui blue book icon"></i>项目名称</label>
                <div class="layui-input-block">
                    <input type="text" name="projectName" required lay-verify="required" autocomplete="off"
                           placeholder="请输入项目名称" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label"><i class="ui blue calendar alternate outline icon"></i>建立时间</label>
                <div class="layui-input-block">
                    <input type="text" name="creatDate" id="creatDate" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label"><i class="ui blue calendar alternate outline icon"></i>结束时间</label>
                <div class="layui-input-block">
                    <input type="text" name="endDate" id="endDate" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">

            <div class="layui-inline">
                <label class="layui-form-label"><i class="ui violet user outline icon"></i>客户ID</label>
                <div class="layui-input-block">
                    <input type="number" name="clientId" id="clientId" required lay-verify="exits" autocomplete="off"
                           placeholder="请输入客户ID" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label"><i class="ui violet user outline icon"></i>文件地址</label>
                <div class="layui-input-block">
                    <input type="text" name="projectAddress" required lay-verify="required" autocomplete="off"
                           placeholder="请输入文件地址" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label"><i class="ui blue font icon"></i>项目类型</label>
                <div class="layui-input-inline">
                    <select name="projectType" lay-verify="required">
                        <option value="">请选择类型</option>
                        <option value="1">一级</option>
                        <option value="2">二级</option>
                        <option value="3">三级</option>
                    </select>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label"><i class="ui violet user outline icon"></i>模型单价</label>
                <div class="layui-input-block">
                    <input type="number" name="modelPrice" required lay-verify="required" autocomplete="off"
                           placeholder="请输入模型单价" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label"><i class="ui violet user outline icon"></i>渲染单价</label>
                <div class="layui-input-block">
                    <input type="number" name="renderPrice" required lay-verify="required" autocomplete="off"
                           placeholder="请输入模型单价" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label"><i class="ui violet user outline icon"></i>后期单价</label>
                <div class="layui-input-block">
                    <input type="number" name="lastPrice" required lay-verify="required" autocomplete="off"
                           placeholder="请输入后期单价" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item" style="margin-left: -110px">

            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="add">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-danger">重置</button>
            </div>
        </div>
    </form>
</div>
<script>
    function isExist() {
        let flag = false;
        $.ajax({
            url: "/client/select",
            method: "post",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({"clientId":Number($("#clientId").val())}),
            dataType: "json",
            async:false,
            success: function (res) {
                if (res.result.length !== 0) {
                    flag = true;
                }
            }
        })

        return flag;
    }

    layui.use(['form', 'layedit', 'laydate'], function () {
        const form = layui.form
            , layer = layui.layer
            , laydate = layui.laydate;
        //日期
        laydate.render({
            elem: '#creatDate'
        });
        laydate.render({
            elem: '#endDate'
        });
        form.render('select');
        //监听提交
        form.verify({
            exits:function (){
                if (!isExist()){
                    return "该客户不存在"
                }
            }
        });
        form.on('submit(add)', function (data) {
            $.ajax({
                url: "/project/add",
                method: "post",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify(data.field),
                dataType: "json",
                success: function () {
                    layer.alert('添加成功', {
                        skin: 'layui-layer-lan' //样式类名
                        , closeBtn: 0
                    });
                }
            })
            return false;
        });
    });
</script>